<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/js/xtable.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/UserRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<form>
    <center>
        <table style="width: 50%;" cellpadding="5" cellspacing="5">
            <tr>
                <td>
                    <select id="marketId" name="marketId" onchange="getUserPage()">
                    </select>
                </td>
                <td>
                    <select id="userType" name="userType" onchange="getUserPage()">
                        <option value="">请选择</option>
                    </select>
                </td>
                <td>
                    <button type="button" onclick="submitUser('')">新增用户</button>
                </td>
            </tr>
        </table>
    </center>
</form>
<br>
<SCRIPT LANGUAGE="JavaScript" >
    var userThead = new Array(
            new XThead("姓名"),
            new XThead("手机号"),
            new XThead("用户类型"),
            new XThead("所属公司"),
            new XThead("状态"),
            new XThead("操作")
    );

    var userXTable = new XTable("user", userThead);

    var state_hash = {
        "0":"冻结",
        "1":"正常",
    }
    var check_hash = {
        "10":"总公司管理员",
        "11":"总公司审核员",
        "30":"市场管理员",
	    "31":"市场员工",
	    "32":"市场检测员",
	    "40":"车商",
        "41":"车商员工"
    };
    userXTable.XTbodyArray = function (model) {
        var array = new Array(
                new XTbody("user", model.userName, "string", null, null, "center"),
                new XTbody("user", model.userMobile, "string", null, null, "center"),
                new XTbody("user", check_hash[model.userType], "string", null, null, "center"),
                new XTbody("user", model.market.marketName, "string", null, null, "center"),
                new XTbody("user", state_hash[model.userState], "string", null, null, "center"),
                new XTbody("user", model.userState == 1?"<a href='#' style='border-right: 1px solid #dddddd' onclick=submitUser('"+ model.userId +"')>编辑&nbsp;</a>" + 
                	"<a href='#' onclick=submitState('"+ model.userId+ "','" + model.accountId+ "',0)>&nbsp;冻结</a>" : 
                	"<a href='#' style='border-right: 1px solid #dddddd' onclick=submitUser('"+ model.userId +"')>编辑&nbsp;</a>" + 
                    "<a href='#' onclick=submitState('"+ model.userId+ "','" + model.accountId+ "',1)>&nbsp;解冻</a>"	
                	, "string", null, null, "center")
        );
        return array;
    }

    userXTable.getXList = function (page){
        var user = new User();
        user.marketId = dwr.util.getValue("marketId");
        user.userType = dwr.util.getValue("userType")
        user.pageIndex = page;
        user.pageSize = userXTable.pageSize;
        UserRpcService.selectUserList(user, function(list){userXTable.addTable(list);});
    }
    function getUserPage() {
        var user = new User();
        user.marketId = dwr.util.getValue("marketId");
        user.userType = dwr.util.getValue("userType");
        UserRpcService.selectUserNum(user, function(num){userXTable.setTotalPage(num);}) ;
    }
</SCRIPT>
</body>
</html>
<script>

    var market = new Market();
    market.marketId = "${marketId}";
    MarketRpcService.selectMarketList(market, callbackSelectMarketList);

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        getUserPage();
    }

    roleList();
    function roleList(){
        var check_hash_market = {
            "32":"市场检测员",
            "30":"市场管理员",
            "31":"市场员工"
        };
        var check_hash_shop = {
            "40":"车商",
            "41":"车商员工"
        };
        if("${marketId}" == "1"){
	        dwr.util.addOptions("userType", check_hash);
	    }else if('${shopId}' == ""){
            dwr.util.addOptions("userType", check_hash_market);
	    }else{
            dwr.util.addOptions("userType", check_hash_shop);
	    }
    }

    function submitUser(userId){
        location = "${ctx}/user/user_manage.jsp?userId=" + userId;
    }
    
    function submitState(userId, accountId, userState){
    	if (userId == null || userId == '' || accountId == null || accountId == '') {
            return false;
        }
    	if(userId == 1){
    		alert("超级管理员不能冻结，冻结后将无法登陆！")
    		return false;
    	}
    	var user = new User();
    	if(userState == 0){
    		if(confirm('确定要冻结用户吗？')){
    			user.userId = userId;
    			user.accountId = accountId;
    			user.userState = userState;
    			UserRpcService.updateUser(user, callbackUpdateUser)
    		}else{
    			return false;
    		}
    	}else{
    		if(confirm('确定要解冻用户吗？')){
    			user.userId = userId;
    			user.accountId = accountId;
    			user.userState = userState;
    			UserRpcService.updateUser(user, callbackUpdateUser)
    		}else{
    			return false;
    		}
    	}
    	
    }
    
    function callbackUpdateUser(data){
    	if(data){
    		alert('操作成功!');
    	}else{
    		alert('操作失败!');
    	}
    	location.reload();
    }
</script>
